package LogicaPresentacion;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import LogicaNegocio.GestionAdministrador;
import LogicaNegocio.GestionAlumnos;
import LogicaNegocio.Grado;

public class verGrados extends JInternalFrame implements ActionListener,Observer
{	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GestionAdministrador GAd;
	private GestionAlumnos GA;
	private String Eleccion, Dni;
	private JTable tbGrados;	
	private JButton Aceptar;
    private JScrollPane scrollPane; 
    private JPanel jpBotones;
	private TablaGrados Tabla = null;        
		
	public verGrados(GestionAdministrador GAd, GestionAlumnos GA, String Eleccion,String Dni, ClaseAObservar observable)
	{
		
		super("Grados", 
	              true, 
	              false, 
	              true, 
	              true);
		setVisible(true);
		setSize(800,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.inicializarSubComponentes();
		this.Eleccion = Eleccion;
		this.Dni= Dni;
		this.GAd = GAd;
		this.GA = GA;
		blnCargarFormulario();
	}
	
	public void blnCargarFormulario()
	{		
		crearTablaProductos();
		this.setLocation(200,200);
		this.setVisible(true);
		inicializarSubComponentes();
	}
	
	public void crearTablaProductos()
	{
		
		tbGrados=null;
		Tabla=new TablaGrados();
		
		if ((Eleccion == "Administrador") ||(Eleccion == "Borrar"))
		{
			Tabla.CrearTabla(GAd.GetGrados());
		}
		if (Eleccion.equals("Alumno"))
		{
			Tabla.CrearTabla(GA.GetGrados());
		}
		
		tbGrados = new JTable(Tabla);
		tbGrados.setPreferredScrollableViewportSize(new Dimension(500, 70));
		tbGrados.setFillsViewportHeight(true);
		tbGrados.setEnabled(true);
		tbGrados.setRowSelectionAllowed(true);
		Tabla.fireTableDataChanged();
				
	}	
	public void inicializarSubComponentes()
	{		
		try
		{
			
			scrollPane= new JScrollPane(tbGrados);

			this.getContentPane().setLayout(new BorderLayout());
	        this.getContentPane().add(scrollPane,"Center");
	        
	        Aceptar=new JButton("Aceptar");
			Aceptar.addActionListener(this);
	      
	        
			jpBotones=new JPanel();
			jpBotones.setLayout(new FlowLayout());
			jpBotones.add(Aceptar);
			
			
			this.getContentPane().add(jpBotones,"South");
		}
		catch(Exception e)
		{
		}
		
	}
		
	public void actionPerformed(ActionEvent e)
	{	
		if (e.getSource() instanceof JButton) 
		{
			JButton btnPulsado=(JButton)e.getSource();

			if (btnPulsado==Aceptar) 
			{
				dispose();
			}					
		}
	}

	public void update(Observable o, Object arg) 
	{	
		tbGrados=null;
		Tabla.CrearTabla(GAd.GetGrados());
		tbGrados = new JTable(Tabla);
		tbGrados.setPreferredScrollableViewportSize(new Dimension(500, 70));
		tbGrados.setFillsViewportHeight(true);
		tbGrados.setEnabled(true);
		tbGrados.setRowSelectionAllowed(true);
		Tabla.fireTableDataChanged();		
	}

	class TablaGrados extends AbstractTableModel
    {
 		/**
 		 * 
 		 */
		private static final long serialVersionUID = 3783586734941958199L;
		private String[] columnNames = {"ID_Grado","Nombre Grado","Caracteristicas de Grado", "InforEconomica","Nombre del campus"};
		private Object[][] data;
	        
	    public void CrearTabla(Map<String,Grado> Grados)
	    {
			
			int cont;
			
			if (Eleccion.equals("Administrador"))
			{
				int filas = Grados.size();
				data=new Object[filas][];
				cont=0;
				Grados = GAd.OrdenarGrados(Grados);
				
	    		for (Map.Entry<String, Grado> entry : Grados.entrySet())
	    		{
	    			
	    			Grado G = Grados.get(entry.getKey());
	    			Object[]a={
	    					   new String(Integer.toString(G.getID())),
	    					   new String(G.getNombreGrado()),
	    					   new String(G.getCaracteristicasGrado()),
	    					   new String(G.getInforEconomica()),
	    					   new String(G.getNombreCampus()),
	
	    			};
	    			data[cont]=a;
	    			cont++;
	    		} 
			}
			
			if (Eleccion.equals("Alumno"))
			{
				cont = 0;
	    		data=new Object[1][];
	    		
				String Grado = GA.VerMisGrados(Dni);
				Grado G = Grados.get(Grado);  

				Object[]a={
						   new String(Integer.toString(G.getID())),
						   new String(G.getNombreGrado()),
						   new String(G.getCaracteristicasGrado()),
						   new String(G.getInforEconomica()),
						   new String(G.getNombreCampus()),
	
				};
				data[0]=a;
			
			}
	    }
	    
	    public int getColumnCount() 
	    {
	        return columnNames.length;
	    }
	
	    public int getRowCount() {
	        return data.length;
	    }
	
	    public String getColumnName(int col) 
	    {
	        return columnNames[col];
	    }
	
	    public Object getValueAt(int row, int col) 
	    {
	        return data[row][col];
	    }
	}
}
